<template>
  <div class="add-version">
    <el-row>
      <el-form :model="form"
               :inline="true"
               ref="form"
               :rules="rules"
               label-width="103px">
        <el-col :span="24"
                class="col-24">
          <el-col :span="24"
                  id="useStyle">
            <el-form-item class="user-style"
                          label="版本号"
                          prop="ver">
              <el-input v-model="form.ver"
                        :disabled="form.sys_ver_id!=undefined?true:false"
                        class="common-style"></el-input>
            </el-form-item>
            <el-form-item class="user-style"
                          label="下载地址"
                          prop="url"
                          v-if="clientType==1?true:false">
              <!-- v-if="clientType==1" -->
              <el-input v-model="form.url"
                        :disabled="form.sys_ver_id!=undefined?true:false"
                        class="common-style"></el-input>
            </el-form-item>
            <el-form-item label="时间"
                          prop="ver_date"
                          class="common-style">
              <el-date-picker v-model="form.ver_date"
                              type="datetime"
                              placeholder="选择日期时间"
                              style="width:100%!important;"></el-date-picker>
            </el-form-item>
            <el-form-item class="update-status"
                          label="更新方式"
                          prop="is_force"
                          v-if="clientType==1?false:true">
              <!-- v-if="clientType==1" -->
              <el-select v-model="form.is_force"
                         placeholder="请选择"
                         @change="updateModeChange"
                         style="width:100%!important;">
                <el-option v-for="item in updateArr"
                           :key="item.value"
                           :label="item.name"
                           :value="item.value"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="更新说明"
                          prop="ver_content"
                          class="common-style">
              <el-input type="textarea"
                        :rows="6"
                        placeholder="请输入内容"
                        v-model="form.ver_content"></el-input>
            </el-form-item>
          </el-col>
        </el-col>
      </el-form>
    </el-row>
    <div :span="24"
         id="footerBox">
      <el-button id="footerCancel"
                 @click="onCancel">取消</el-button>
      <el-button type="primary"
                 @click="onSave('form')">保存</el-button>
    </div>
  </div>
</template>
<script>
import { versionManagementApi } from "@/services/SystemSetting";
import { standardCodeApi } from "@/services/StandardCode";
export default {
  props: {
    catchModifySave: Function,
    catchModifyBack: Function,
    todos: Number,
    dialogForm: Object,
    clientType: Number
  },
  // 监听
  watch: {
    todos (ne, old) {
      // console.log('新值', ne, old);
      if (ne == 1) {
        var rowObj = JSON.parse(JSON.stringify(this.dialogForm));
        this.form.ver = rowObj.ver;
        this.form.sys_ver_id = rowObj.sys_ver_id;
        this.form.ver_content = rowObj.ver_content;
        this.form.system_type = rowObj.system_type;
        this.form.is_force = rowObj.is_force;
        this.form.url = rowObj.url;
        this.form.ver_date = rowObj.ver_date;
        // console.log("1watch-子组件=", ne, old, 'rowObj=', rowObj, 'clientType=', this.clientType);
      } else {
        this.form.ver = '';
        this.form.ver_content = '';
        this.form.system_type = '';
        this.form.is_force = '';
        this.form.url = '';
        this.form.ver_date = '';
        //console.log('0watch-clientType=', this.clientType, 'todos=', ne, 'dialogForm=', this.dialogForm)
      }

    }
  },
  data () {
    return {
      codeOne: "0",
      form: {
        sys_ver_id: "",
        ver: "",
        ver_content: "",
        system_type: "",
        is_force: "",
        url: "",
        ver_date: ""
      },
      updateArr: [],
      clientArr: [],
      fileList: [],
      rules: {
        versionNo: [
          { required: true, message: "版本号不能为空", trigger: "blur" }
        ],
        type: [{ required: true, message: "客户端不能为空", trigger: "blur" }],
        apk: [{ required: true, message: "apk不能为空", trigger: "blur" }],
        updateMode: [
          { required: true, message: "更新方式不能为空", trigger: "blur" }
        ],
        publishTime: [
          { required: true, message: "发布时间不能为空", trigger: "blur" }
        ],
        updateNotes: [
          { required: true, message: "更新说明不能为空", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    updateModeChange (val) {
      this.form.is_force = val;
      console.log('更新方式=', val, this.updateArr)
    },
    // 上传文件
    handleRemove (file, fileList) {
      console.log(file, fileList);
    },
    handlePreview (file) {
      console.log(file);
    },
    handleExceed (files, fileList) {
      this.$message.warning(
        `当前限制选择 3 个文件，本次选择了 ${
        files.length
        } 个文件，共选择了 ${files.length + fileList.length} 个文件`
      );
    },
    beforeRemove (file, fileList) {
      return this.$confirm(`确定移除 ${file.name}？`);
    },
    // 取消事件
    onCancel () {
      this.form.ver = '';
      this.form.sys_ver_id = '';
      this.form.ver_content = '';
      this.form.system_type = '';
      this.form.is_force = '';
      this.form.url = '';
      this.form.ver_date = '';
      this.catchModifyBack("0");
    },
    // 保存事件
    onSave (data) {
      this.$refs[data].validate(valid => {
        if (valid) {
          if (this.form.sys_ver_id == undefined) {
            versionManagementApi.getPublish({
              data: {
                ver: this.form.ver,
                ver_content: this.form.ver_content,
                system_type: this.clientType,
                is_force: this.form.is_force,
                url: this.form.url,
                ver_date: this.form.ver_date
              }
            }).then(res => {
              if (res.code == 0) {
                this.catchModifySave("0");
              }
            }).catch(error => {
              console.log(error);
            });
          } else {
            versionManagementApi.editVersionList({
              data: this.form
            }).then(res => {
              if (res.code == 0) {
                this.$message.success('操作成功!');
                this.catchModifySave("0");
              }
            }).catch(error => {
              console.log(error);
            });
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    //获取更新方式标准码
    getUpdateTypeData () {
      standardCodeApi
        .getStandardCodeData({
          data: {
            code_type: "updateType"
          }
        })
        .then(res => {
          this.updateArr = res.data.list;
        })
        .catch(error => {
          console.log(error);
        });
    },
    //获取客户端类型标准码
    getClientTypeData () {
      standardCodeApi
        .getStandardCodeData({
          data: {
            code_type: "clientType"
          }
        })
        .then(res => {
          this.clientArr = res.data.list;
        })
        .catch(error => {
          console.log(error);
        });
    },
    // 列表数据
    getData () { }
  },
  created () {
    //console.log('created-clientType=', this.clientType, 'todos=', this.todos, 'dialogForm=', this.dialogForm);
    var rowObj = JSON.parse(JSON.stringify(this.dialogForm));
    this.form.ver = rowObj.ver;
    this.form.ver_content = rowObj.ver_content;
    this.form.system_type = rowObj.system_type;
    this.form.is_force = rowObj.is_force;
    this.form.url = rowObj.url;
    this.form.sys_ver_id = rowObj.sys_ver_id;
    this.form.ver_date = rowObj.ver_date;
    this.getUpdateTypeData();
    this.getClientTypeData();
  },
  mounted () {
    // this.getData();
  }
};
</script>
  <style lang="scss"  scoped>
#footerCancel {
  margin-right: 13px;
}
#footerBox {
  text-align: center;
}
.col-24 {
  margin-top: 20px;
}
.common-style {
  width: 100% !important;
}
.add-style {
  font-size: 18px;
  //font-family: SimSun;
  font-weight: 400;
}
.add-version {
  height: auto;
  /deep/ .el-form-item__content {
    line-height: 40px;
    position: relative;
    font-size: 14px;
    width: 83% !important;
  }
  // padding: 0 20px;
}
.user-style {
  width: 100% !important;
  /deep/ .el-form-item__label {
    text-align: left !important;
    float: left;
    font-size: 14px;
    font-family: MicrosoftYaHei;
    font-weight: 400;
    color: #333 !important;
    line-height: 40px;
    padding: 0 12px 0 0;
    box-sizing: border-box;
    margin-top: 0px !important;
  }
}

.update-status {
  width: 100% !important;
  .el-input-group__append button.el-button,
  .el-input-group__append div.el-select .el-input__inner,
  .el-input-group__append div.el-select:hover .el-input__inner,
  .el-input-group__prepend button.el-button,
  .el-input-group__prepend div.el-select .el-input__inner,
  .el-input-group__prepend div.el-select:hover .el-input__inner {
    border-color: transparent;
    background-color: white !important;
    color: #1890ff;
    border-top: 0;
    border-bottom: 0;
  }
  .el-input-group__append button.el-button:hover {
    border: 1px solid #1890ff !important;
  }
}
#useStyle {
  /deep/ .el-form-item__label {
    text-align: left;
    float: left;
    font-size: 14px;
    color: #606266;
    line-height: 40px;
    padding: 0 12px 0 0;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    margin-top: 0px !important;
  }
}
.add-version {
  .el-form-item__content {
    line-height: 40px;
    position: relative;
    font-size: 14px;
    width: 83% !important;
  }
  //padding: 0 20px;
}
</style>
